/**
  *@File文件:BillController.java
  *@Date日期:2017年7月18日 下午1:46:59
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiaomengapp.bean.Bill;
import com.qiaomengapp.bean.BillAndBillGood;
import com.qiaomengapp.bean.BillAndBillGoodWrapper;
import com.qiaomengapp.bean.GoodDetail;
import com.qiaomengapp.bean.Provider;
import com.qiaomengapp.bean.User;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IBillGoodService;
import com.qiaomengapp.service.IBillManageService;

/**
 *@Description描述:
 *@Author作者 wangzongwen
 *@Date日期:2017年7月18日
 */
@Controller
@RequestMapping("bill/api")
public class BillController {
	@Autowired
	private IBillManageService iBillManageService;
	
	@Autowired
	private IBillGoodService iBillGoodService;
	
	
	/**
	 *@Description描述: 根据订单状态查询订单
	 *@param status 要查询的订单状态
	 *@return
	 *@return List<Bill> 符合条件的订单集合
	 * @throws JsonProcessingException 
	 */
	@RequestMapping("/selectBillByStatus/{status}")
	@ResponseBody
	public Map<String, List<GoodDetail>> selectBillByStatus(@PathVariable("status") Integer status,HttpSession session) throws JsonProcessingException {
		//得到用户对象
		User user=(User) session.getAttribute("user");
		if (user==null) {
			user=new User();
			user.setId(1);
		}
		//保存订单和订单商品信息
		Map<String, List<GoodDetail>> map= new HashMap<String, List<GoodDetail>>();
		List<Bill> list = iBillManageService.selectBillByStatus(status,user.getId());
		//把订单Bill对象转换为字符串
		ObjectMapper objectMapper = new ObjectMapper();
		//根据订单编号查询订单商品
		for (Bill bill : list) {
			List<GoodDetail> goodDetails = iBillGoodService.selectBillGood(bill.getBillCode());
			String billStr = objectMapper.writeValueAsString(bill);
			System.out.println(billStr);
			map.put(billStr, goodDetails);
		}
		
		return map;
	}
	
	/**
	 *@Description描述: 创建订单
	 *@param billAndBillGoodWrapper 创建订单需要的数据
	 *@return
	 *@return String 跳转的地址
	 */
	@RequestMapping(value="/createBill",produces={"text/html;charset=UTF-8"})
	@ResponseBody
	public String createBill(BillAndBillGoodWrapper billAndBillGoodWrapper,HttpSession session){
		//得到用户id
		User user=(User) session.getAttribute("user");
		Integer userId;
		if (user!=null) {
			userId = user.getId();			
		}else{
			userId=1;
		}
		//遍历订单和订单商品集合
		List<BillAndBillGood> billAndBillGoods = billAndBillGoodWrapper.getBillAndBillGoods();
		for (BillAndBillGood billAndBillGood : billAndBillGoods) {
			//添加订单userId
			Bill bill = billAndBillGood.getBill();
			bill.setUserId(userId);
		}
		
		try {
			iBillManageService.insertBill(billAndBillGoodWrapper);
			return "{success:\"订单创建成功！\"}";
		} catch (CustomException e) {
			return "{error:\""+e.getMessage()+"\"}";
		}
	}
	
	/**
	 *@Description描述: 根据订单编号查询供应商
	 *@param billCode 要查询的订单编号
	 *@return
	 *@return Provider 查到的供应商信息
	 */
	@RequestMapping("/selectProviderByBillCode/{billCode}")
	@ResponseBody
	public Provider selectProviderByBillCode(@PathVariable("billCode") String billCode){
		Provider provider = iBillManageService.selectProviderByBillCode(billCode);
		return provider;
	}
	
	/**
	 *@Description描述: 根据订单编号查询订单
	 *@param billCode 要查询的订单编号
	 *@return
	 *@return Bill 查询到的订单
	 * @throws JsonProcessingException 
	 */
	@RequestMapping("/selectBillByBillCode/{billCode}")
	@ResponseBody
	public Map<String, List<GoodDetail>> selectBillByBillCode(@PathVariable("billCode") String billCode) throws JsonProcessingException{
		Bill bill = iBillManageService.selectBillByCode(billCode);
		if (bill==null) {
			return null;
		}
		//根据订单编号查询订单商品
		List<GoodDetail> goodDetails = iBillGoodService.selectBillGood(billCode);
		//把订单Bill对象转换为字符串
		ObjectMapper objectMapper = new ObjectMapper();
		String billStr = objectMapper.writeValueAsString(bill);
		
		//保存数据
		Map<String,List<GoodDetail>> map = new HashMap<String,List<GoodDetail>>();
		map.put(billStr, goodDetails);
		return map;
	}
}
